<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

include('includes/Admin.Core.php');

CheckAccess('manageusergroup');


PrintHeader('用户群组');


$action = ForceIncomingString('action', 'displaydefault');

$dontdeleteids = array(1, 2, 3, 4);

// ############################## DELETE USERGROUP #############################

if($action == 'deleteusergroup')
{
	$groupid = ForceIncomingInt('groupid');

	if(IsPost('confirmcancel'))
	{
		$action = 'displaydefault';

	}else if(IsPost('confirmdelete'))	{
		if(!in_array($groupid, $dontdeleteids)){
			$DB->query("DELETE FROM " . TABLE_PREFIX . "usergroups WHERE groupid = '" . $groupid . "'");
			$DB->query("UPDATE " . TABLE_PREFIX . "users SET groupid = 3 WHERE groupid = '" . $groupid . "'");
		}

		PrintRedirect('admin.usergroups.php', 1);
	}else{

		$usergroup = $DB->query_first("SELECT name FROM " . TABLE_PREFIX . "usergroups WHERE groupid = $groupid");

		PrintSection('删除用户群组');

		echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
		<tr><td class="tdrow1" colspan="2">确认删除</td></tr>
		<tr>
		<td class="tdrow2" width="60%" valign="top"><b>确定删除用户群组: "<font class=ohredb>' . $usergroup['name'] . '</font>" 吗?</b><br /><br />
		注: 删除群组后, 属于此群组的所有用户将自动转入"<font class=ohblueb>注册用户</font>"组.</td>
		<td class="tdrow3" valign="top">
		<form method="post" action="admin.usergroups.php">
		<input type="hidden" name="groupid" value="' . $groupid . '" />
		<input type="hidden" name="action" value="deleteusergroup" />
		<input type="submit" name="confirmdelete" value=" 确定删除 " />&nbsp;&nbsp;
		<input type="submit" name="confirmcancel" value=" 取消 " />
		</form>
		</td>
		</tr>
		</table>';
		EndSection();

	}

}



// ############################# INSERT OR EDIT USERGROUP ###########################

if($action == 'insertusergroup' OR $action == 'updateusergroup')
{
	$groupid = ForceIncomingInt('groupid');
	$name  = ForceIncomingString('name');
	$allowview = ForceIncomingInt('allowview');
	$allowlogin = ForceIncomingInt('allowlogin');
	$allowupload = ForceIncomingInt('allowupload');
	$allowcomment = ForceIncomingInt('allowcomment');
	$allowdownload = ForceIncomingInt('allowdownload');
	$allowdelete = ForceIncomingInt('allowdelete');
	$allowuploadshow = ForceIncomingInt('allowuploadshow');
	$allowcommentshow = ForceIncomingInt('allowcommentshow');

	$manage = ForceIncomingInt('manage');
	$managesetting = ForceIncomingInt('managesetting');
	$managecomment = ForceIncomingInt('managecomment');
	$managecategory = ForceIncomingInt('managecategory');
	$manageuser = ForceIncomingInt('manageuser');
	$manageusergroup = ForceIncomingInt('manageusergroup');
	$managenews = ForceIncomingInt('managenews');
	$managenotice = ForceIncomingInt('managenotice');
	$managelanguage = ForceIncomingInt('managelanguage');
	$manageimage = ForceIncomingInt('manageimage');
	$managedatabase = ForceIncomingInt('managedatabase');
	$managetemplate = ForceIncomingInt('managetemplate');
	$manageupgrade = ForceIncomingInt('manageupgrade');

	if ($name == '')
	{
		$errors = "群组名称不能为空!";
	}

	if($errors){

		if($action == 'insertusergroup'){
			PrintErrors($errors, '创建群组错误');
			$action = 'createusergroup';
		}else if($action == 'updateusergroup'){
			PrintErrors($errors, '编辑群组错误');
			$action = 'editusergroup';
		}

	}else{
		if($action == 'insertusergroup'){
			$DB->query("INSERT INTO " . TABLE_PREFIX . "usergroups VALUES (NULL, '$name', '$allowview', '$allowlogin', '$allowupload', '$allowcomment', '$allowdownload', '$allowdelete', '$allowuploadshow', '$allowcommentshow', '$manage', '$managesetting', '$managecomment', '$managecategory', '$manageuser', '$manageusergroup', '$managenews', '$managenotice', '$managelanguage', '$manageimage', '$managedatabase', '$managetemplate', '$manageupgrade') ");
		}else if($action == 'updateusergroup'){
			$DB->query("UPDATE " . TABLE_PREFIX . "usergroups SET 
			name= '$name',
			allowview     = '$allowview',
			allowlogin     = '$allowlogin',
			allowupload     = '$allowupload',
			allowcomment     = '$allowcomment',
			allowdownload     = '$allowdownload',
			allowdelete     = '$allowdelete',
			allowuploadshow     = '$allowuploadshow',
			allowcommentshow     = '$allowcommentshow',
			manage     = '$manage',
			managesetting     = '$managesetting',
			managecomment     = '$managecomment',
			managecategory     = '$managecategory',
			manageuser     = '$manageuser',
			manageusergroup     = '$manageusergroup',
			managenews     = '$managenews',
			managenotice     = '$managenotice',
			managelanguage     = '$managelanguage',
			manageimage     = '$manageimage',
			managedatabase     = '$managedatabase',
			managetemplate    = '$managetemplate',
			manageupgrade     = '$manageupgrade'
			WHERE groupid           = '$groupid'");
		}

		PrintRedirect('admin.usergroups.php', 1);
	}

}

// ############################## DISPLAY DEFAULT ##############################

if($action == 'displaydefault')
{
	$getusergroups = $DB->query("SELECT ug.groupid, ug.name, COUNT(u.userid) AS users FROM " . TABLE_PREFIX . "usergroups ug LEFT JOIN " . TABLE_PREFIX . "users u ON (u.groupid = ug.groupid) GROUP BY ug.groupid ORDER BY ug.groupid");

	PrintSection('创建用户组');
	echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" colspan="2">创建用户组</td>
	</tr>
	<tr>
	<td class="tdrow2" width="60%">创建新的用户群组:</td>
	<td class="tdrow3">
	<form method="post" action="admin.usergroups.php">
	<input type="hidden" name="action" value="createusergroup" />
	<input type="submit" value=" 创建群组 " />
	</form>
	</td>
	</tr>
	</table>';
	EndSection();

	PrintSection('群组权限设置');

	echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" colspan="3">群组权限说明</td>
	</tr>
	<tr>
	<td class="tdrow3" colspan="3">默认安装后, 群组权限说明:<br/>1. <b>系统管理员</b>: 具有全部网站前后台权限.<br/>2. <b>注册用户</u></b>: 具有网站前台上传图片, 发表评论, 文章等权限.<br/>3. <b>禁止登录</b>: 无法登录网站前台, 即无任何权限.<br/>4. <b>游客</b>: 指未注册用户, 具有查看权限. <font class=ohred>严禁设置游客具有管理权限.</font></td>
	</tr>
	<tr>
	<td class="tdrow1">群组名称</td>
	<td class="tdrow1">用户数量</td>
	<td class="tdrow1">删除</td>
	</tr>';

    while($usergroup = $DB->getrow($getusergroups))
    {
		echo '<tr>
		<td class="tdrow2"><a href="admin.usergroups.php?action=editusergroup&groupid=' . $usergroup['groupid'] . '">' . $usergroup['name'] . '</a>&nbsp;(ID: '.$usergroup['groupid'].')</td>
		<td class="tdrow3">' . $usergroup['users'] . '</td>
		<td class="tdrow2">' . Iif(in_array($usergroup['groupid'], $dontdeleteids), '<img src="' . TEMPLATEPATH . 'images/notrash.gif" />', '<a href="admin.usergroups.php?action=deleteusergroup&groupid=' . $usergroup['groupid'] . '"><img src="' . TEMPLATEPATH . 'images/trash.gif" /></a>') . '</td>
		</tr>';
    }


	echo '</table>';
	EndSection();
}


// ########################## DISPLAY USERGROUP FORM ###########################

if($action == 'createusergroup' OR $action == 'editusergroup')
{
	if(isset($errors))
	{
		$usergroup = array('groupid'   => $groupid,
		'name'   => $name,
		'allowview'     => $allowview,
		'allowlogin'     => $allowlogin,
		'allowupload'     => $allowupload,
		'allowcomment'     => $allowcomment,
		'allowdownload'     => $allowdownload,
		'allowdelete'     => $allowdelete,
		'allowuploadshow'     => $allowuploadshow,
		'allowcommentshow'     => $allowcommentshow,
		'manage'     => $manage,
		'managesetting'     => $managesetting,
		'managecomment'     => $managecomment,
		'managecategory'     => $managecategory,
		'manageuser'     => $manageuser,
		'manageusergroup'     => $manageusergroup,
		'managenews'     => $managenews,
		'managenotice'     => $managenotice,
		'managelanguage'     => $managelanguage,
		'manageimage'     => $manageimage,
		'managedatabase'     => $managedatabase,
		'managetemplate'     => $managetemplate,
		'manageupgrade'     => $manageupgrade);

	}else if($action == 'editusergroup'){
		$groupid = ForceIncomingInt('groupid');
		$usergroup = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "usergroups WHERE groupid = '$groupid'");
	}else{
		$usergroup = array();
	}

	if($groupid)
	{
		PrintSection('编辑群组: ' . $usergroup['name']);
	}else{
		PrintSection('创建群组');
	}

	echo '<form method="post" action="admin.usergroups.php" name="usergroups">
	<input type="hidden" name="action" value="' . Iif($action == 'createusergroup', 'insertusergroup', 'updateusergroup') . '" />
	<input type="hidden" name="groupid" value="' . $usergroup['groupid'] . '" />

	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" colspan="2">群组名称</td>
	</tr>
	<tr>
	<td class="tdrow2" width="50%">输入用户群组的名称(<font class=ohred>必填项</font>):</td>
	<td class="tdrow3"><input type="text" name="name" value="' . $usergroup['name'] . '"  size="30" /></td>
	</tr></table>';
	EndSection();


	PrintSection('网站前台权限');
	echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr><td class="tdrow3"><input type="checkbox" name="allowlogin" value="1" ' . Iif($usergroup['allowlogin'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<b>允许登录</b>: 是否允许登录网站前台?</td>	
	</tr>
	<tr><td class="tdrow3"><input type="checkbox" name="allowview" value="1" ' . Iif($usergroup['allowview'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<b>允许查看</b>: 是否允许查看网站图片的大图?</td>	
	</tr>
	<tr><td class="tdrow3"><input type="checkbox" name="allowupload" value="1" ' . Iif($usergroup['allowupload'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<b>允许上传</b>: 是否允许上传图片及添加图片信息?</td>	
	</tr>
	<tr><td class="tdrow3"><input type="checkbox" name="allowuploadshow" value="1" ' . Iif($usergroup['allowuploadshow'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<b>允许立即发布图片</b>: 用户上传图片后是否立即显示图片? <font class=ohred>如果没有此权限, 则需要管理员审核后才能发布</font>.</td>	
	</tr>
	<tr><td class="tdrow3"><input type="checkbox" name="allowdelete" value="1" ' . Iif($usergroup['allowdelete'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<b>允许删除</b>: 是否允许用户删除自己发布的图片?</td>	
	</tr>
	<tr><td class="tdrow3"><input type="checkbox" name="allowdownload" value="1" ' . Iif($usergroup['allowdownload'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<b>允许下载</b>: 是否允许下载图片? <font class=ohred>如果有此权限, 则用户可以下载属于该用户组的未添加水印的原始图片(不包括用于销售的图片)</font>.</td>
	</tr>
	<tr><td class="tdrow3"><input type="checkbox" name="allowcomment" value="1" ' . Iif($usergroup['allowcomment'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<b>允许评论</b>: 是否允许发表评论?</td>	
	</tr>
	<tr><td class="tdrow3"><input type="checkbox" name="allowcommentshow" value="1" ' . Iif($usergroup['allowcommentshow'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<b>允许立即发布评论</b>: 用户发表评论后是否立即显示评论? <font class=ohred>如果没有此权限, 则需要管理员审核后才能发布</font>.</td>	
	</tr>
	</table>';
	EndSection();


	PrintSection('<font class="ohblueb">后台管理权限</font>');
	echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr><td class="tdrow2"><input type="checkbox" name="manage" value="1" ' . Iif($usergroup['manage'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许登录</font>: 是否允许登录后台管理? <font class=ohred>无此权限则其它管理权限无效</font>.</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="managesetting" value="1" ' . Iif($usergroup['managesetting'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许系统设置</font>: 是否允许进行网站的系统设置?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="manageuser" value="1" ' . Iif($usergroup['manageuser'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许管理用户</font>: 是否允许管理用户?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="manageusergroup" value="1" ' . Iif($usergroup['manageusergroup'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许管理用户群组</font>: 是否允许管理用户群组?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="manageimage" value="1" ' . Iif($usergroup['manageimage'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许管理图片</font>: 是否允许管理图片?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="managecategory" value="1" ' . Iif($usergroup['managecategory'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许管理图片目录</font>: 是否允许管理图片的分类目录?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="managetemplate" value="1" ' . Iif($usergroup['managetemplate'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许管理模板</font>: 是否允许在线编辑网站前台模板?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="managelanguage" value="1" ' . Iif($usergroup['managelanguage'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许管理语言</font>: 是否允许在线编辑网站前台语言?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="managenews" value="1" ' . Iif($usergroup['managenews'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许管理文章</font>: 是否允许管理网站的新闻或文章?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="managenotice" value="1" ' . Iif($usergroup['managenotice'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许管理公告</font>: 是否允许管理网站公告?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="managecomment" value="1" ' . Iif($usergroup['managecomment'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许管理评论</font>: 是否允许管理用户发表的评论?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="managedatabase" value="1" ' . Iif($usergroup['managedatabase'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许数据维护</font>: 是否允许备份, 恢复网站数据库等?</td>	
	</tr>
	<tr><td class="tdrow2"><input type="checkbox" name="manageupgrade" value="1" ' . Iif($usergroup['manageupgrade'] == 1, ' checked="checked"') .' />&nbsp;&nbsp;<font class=ohblueb>允许系统升级</font>: 是否允许进行系统升级操作?</td>	
	</tr>
	</table>';
	EndSection();

	echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
	<td align="center"><input type="submit" name="" value="' . Iif($action == 'createusergroup', ' 创建群组 ', ' 保存更新 ') . '" />
	</tr>
	</table>
	</form>';
  

}

// ############################### PRINT FOOTER ################################

PrintFooter();

?>